package D01_03;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.*;

public class Demo6 {
    public static void main(String[] args) throws Exception {
        List<Dept> depts = getDepts();
        List<Emp> topEmps = getTopEmps(depts);
        for (Dept dept : depts) {
            for (Emp topEmp : topEmps) {
                if (dept.getDid().equals(topEmp.getDid())){
                    System.out.println(dept.getDname()+" "+topEmp.getEname()+" "+topEmp.getEsalary());
                }
            }
        }
        List<Map<String, String>> evg = getEvg(depts);
        for (Map<String, String> map : evg) {
            for (Map.Entry<String, String> e : map.entrySet()) {
                System.out.println(e.getKey()+" "+e.getValue());
            }
        }
        searchEmp("e04");
    }
    public static List<Dept> getDepts() throws DocumentException {
        List<Dept> depts = new ArrayList<>();
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read("src/Y2020/D01_03/dept.xml");
        Document read1 = saxReader.read("src/Y2020/D01_03/emp.xml");
        Element root = read.getRootElement();
        Element root1 = read1.getRootElement();
        List<Element> dept = root.elements("dept");
        List<Element> emp = root1.elements("emp");
        for (Element d : dept) {
            Dept dept1 = new Dept();
            dept1.setDid(d.elementText("dept-id"));
            dept1.setDname(d.elementText("dept-name"));
            ArrayList<Emp> emps = new ArrayList<>();
            o:for (Element e : emp) {
                if (d.elementText("dept-id").equals(e.elementText("dept-id"))){
                    for (Emp emp1 : emps) {
                        if (emp1.getEid().equals(e.elementText("emp-id"))){
                            continue o;
                        }
                    }
                    Emp emp1 = new Emp();
                    emp1.setDid(e.elementText("dept-id"));
                    emp1.setEid(e.elementText("emp-id"));
                    emp1.setEname(e.elementText("emp-name"));
                    emp1.setEsalary(Integer.parseInt(e.elementText("emp-salary")));
                    emps.add(emp1);
                }
            }
            dept1.setEmps(emps);
            depts.add(dept1);
        }
        return depts;
    }
    public static List<Emp> getTopEmps(List<Dept> list){
        List<Emp> emps = new ArrayList<>();
        int small = list.get(0).getEmps().get(0).getEsalary();
        for (Dept dept : list) {
            for (Emp emp : dept.getEmps()) {
                if (emp.getEsalary()<small){
                    small = emp.getEsalary();
                }
            }
        }
        for (Dept dept : list) {
            for (Emp emp : dept.getEmps()) {
                if (emp.getEsalary()==small){
                    emps.add(emp);
                }
            }
        }
        return emps;
    }
    public static List<Map<String,String>> getEvg(List<Dept> list){
        List<Map<String,String>> l = new ArrayList<>();
        for (Dept dept : list) {
            int a = 0;
            Map<String,String> map = new HashMap<>();
            for (Emp emp : dept.getEmps()) {
                a+=emp.getEsalary();
            }
            map.put(dept.getDname(),a/dept.getEmps().size()+"");
            l.add(map);
        }
        l.sort(new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                int a = 0;
                int b = 0;
                for (Map.Entry<String, String> e : o1.entrySet()) {
                    a = Integer.parseInt(e.getValue());
                }
                for (Map.Entry<String, String> e : o2.entrySet()) {
                    b = Integer.parseInt(e.getValue());
                }
                return b-a;
            }
        });
        return l;
    }
    public static void searchEmp(String empId) throws DocumentException {
        List<Dept> depts = getDepts();
        for (Dept dept : depts) {
            for (Emp emp : dept.getEmps()) {
                if (emp.getEid().equals(empId)){
                    System.out.println(emp.getEname()+" "+emp.getEsalary());
                    return;
                }
            }
        }
        System.out.println("没有找到");
    }
}
